#!/usr/bin/env python3

# this is a dbus server.

import sys

from PyQt5.QtCore import QCoreApplication, pyqtSlot, QObject
from PyQt5.QtDBus import QDBusConnection, QDBusInterface, QDBusReply, QDBusAbstractAdaptor

class Pong(QObject):
    @pyqtSlot(str, result=str)
    def pong_method(self, args):
        print("Get a proxy-method call... the args is: ", args)
        return 'ping("%s") got called.' % args

    @pyqtSlot()
    def pong_method_without_params(self):
        print("Get a proxy-method call...")


if __name__ == "__main__":
    app = QCoreApplication(sys.argv)
    bus = QDBusConnection.sessionBus()  # sessionBus()
    if not bus.isConnected():
        raise Exception("Fail to connect to Session-Bus.")
    # register the service
    if not bus.registerService('org.example.QtDBus.PingPong'):  # 注册 BusName
        raise Exception("Fail to register the service.")

    pong = Pong()  # 创建对象 -> dbus server object
    print("-->> ", isinstance(pong, QDBusAbstractAdaptor))
    bus.registerObject("/",  # this is the object-path, must be start from '/'
                        "pong.anything.namespace",  # this is the interface [choose to name it]
                        pong,  # this is the server obj, but you must prepare it before
                        # QDBusConnection.ExportAllSlots)  # 注册所有slot作为proxy-method
                        QDBusConnection.ExportAllContents)
    sys.exit(app.exec_())
